#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define str string
#define fi first
#define se second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define MP make_pair
#define ms(a,t) memset(a,t,sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) ((x)&(-(x)))
const ll INF = 0x3f3f3f3f;
const int maxn = 4e5+10;
const int maxm = 2e5+10;
const db eps = 1e-6;
int n,m,K;
ll a[maxn],c[maxn],k[maxn];
bool q[maxn];
ll calc(ll l,ll r) {
	return (l+r)*(r-l+1)/2;
}
char op[20][maxn];
void init() {
	cin >> n >> m; K = 0;
	while((1<<K) < n) K++;
	k[0] = 0;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		k[i] = k[i-1];
		while((1<<k[i])<i) k[i]++;
	}
	for(int i = 1; i <= m; i++) {
		cin >> c[i]; q[c[i]] = true;
	}
	for(int i = 1; i <= K; i++) {
		for(int j = 0; j < (1<<(K-i)); j++) {
			cin >> op[i][j];
		}
	}
}
ll b[maxn];
pii f[20][maxn];
pii cur[maxn]; int tp;
void update(int p,int dep) {
	if(dep == 0) {
		f[dep][p] = MP(b[p+1],p+1);
	} else {
		tp--;
		if(op[dep][p] == '0') {
			if(f[dep-1][p<<1].fi >= dep) f[dep][p] = f[dep-1][p<<1];
			else f[dep][p] = f[dep-1][p<<1|1];
		} else {
			if(f[dep-1][p<<1|1].fi >= dep) f[dep][p] = f[dep-1][p<<1|1];
			else f[dep][p] = f[dep-1][p<<1];
		}
	}
//	cerr<<p<<' '<<dep<<' '<<f[dep][p]<<'\n';
	if((p&1) == 0) {
		cur[++tp] = MP(dep,p);
		return;
	}
	update(p/2,dep+1);
}
ll query(int v) {
//	for(int i = 1; i <= tp; i++) cerr<<cur[i].fi<<' '<<cur[i].se<<"  ";	cerr<<'\n';
	if(tp == 1) {
		return f[cur[1].fi][cur[1].se].se;
	}
	ll ret = 0;
	priority_queue<pii ,vector<pii >,greater<pii > > pq; // val which still can win
//	cerr<<f[cur[tp].fi][cur[tp].se]<<'\n';
	pq.push(f[cur[tp].fi][cur[tp].se]);
	ll l = INF,pos = cur[tp].se; // [l,i] which still can win
//	cerr<<v<<' '<<cur[tp].fi+1<<' '<<f[cur[tp].fi][cur[tp].se]<<'\n';
	for(int i = cur[tp].fi+1; i <= v;i ++) {
		char o = op[i][pos/2];
//		cerr<<i<<' '<<pos<<' '<<o<<' '<<l<<' '<<pq.size()<<'\n';
//		if(!pq.empty()) cerr<<pq.top().se<<'\n';
		if((pos&1)) { // this is right son
			if(o == '0') {
				if(f[i-1][pos-1].fi >= i) {
					while(!pq.empty()) pq.pop();
					pq.push(f[i-1][pos-1]);
				}
			} else {
				if(l == INF && !pq.empty() && pq.top().fi >= i) ;
				else {
					while(!pq.empty() && pq.top().fi < i) pq.pop();
					pq.push(f[i-1][pos-1]);
				}
			}
		} else { // this is left son
//			cerr<<i<<' '<<o<<"??\n";
			if(o == '0') {
				if(!pq.empty() && pq.top().fi < i) l = ((pos+1)<<(i-1));
				while(!pq.empty() && pq.top().fi < i) pq.pop();
			} else {
				if(l == INF) l = ((pos+1)<<(i-1));
			}
		}
		pos /= 2;
	}
//	cerr<<l<<' '<<(1<<v)<<'\n';
	while(!pq.empty()) { ret += pq.top().se; pq.pop(); }
	if(l == INF) return ret;
	return ret + calc(l+1,(1ll<<v));
}
int x[4];
ll ans[maxn];
void Solve() {
	cin >> x[0] >> x[1] >> x[2] >> x[3];
	for(int i = 1; i <= n; i++) {
		b[i] = 0;
//		cerr<<(a[i] ^ x[i%4])<<' ';
	}
//	cerr<<'\n';
	tp = 0;
	for(int i = 1; i <= n; i++) {
		b[i] = (a[i] ^ x[i%4]);
		update(i-1,0);
		if(q[i]) ans[i] = query(k[i]);
	}
//	for(int i = 1; i <= n; i++) { cerr<<ans[i]<<' '; } cerr<<'\n';
	ll ret = 0;
	for(int i = 1; i <= m; i++) {
		ret ^= (i*ans[c[i]]);
	}
	cout<<ret<<'\n';
}
/*

*/
int main() {
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	init();

	int tt; cin >> tt; while(tt--)
	Solve();
	return 0;
}
